﻿#region VSS
/* 
 * 页面名称：Commodity
 * 页面创建人：Administrator
 * 页面创建时间：06/24/2008 12:53:42
 *********************************************
 * VSS信息
 * $Header$ 
 * $History$
 */
#endregion


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace MyCollection
{
    //************************************************************************
    // 用户名				上海淘游电子商务有限公司
    // 系统名				X2HiDotNet Program
    // 子系统名           商品类      
    // 类说明                   	
    // 作成者					James
    // 改版日					06/24/2008 12:53:42
    // 改版内容					新建
    //************************************************************************

    public class Commodity
    {
        #region 构造函数
        public Commodity(MyCollectDataSet.CollectProgramRow cl, string commUrl)
        {
            myCollectPropert = cl;
            this.commUrl = commUrl;
            this.comHtmlCode = CollectClass.GetRemoteHtmlCode(commUrl, cl.IsUtf);
        }

        #endregion

        #region 成员变量
        //private string title, imgPath, content;
        //private double price;

        private string localImgPath;

        public string LocalImgPath
        {
            get { return localImgPath; }
            set { localImgPath = value; }
        }

        private MyCollectDataSet.CollectProgramRow myCollectPropert;
        private string comHtmlCode, commUrl;

        /// <summary>
        /// 商品地址
        /// </summary>
        public string CommUrl
        {
            get { return commUrl; }
            set { commUrl = value; }
        }

        private string GetContent(Match match)
        {
            string imgurl = match.Groups["picture"].ToString();
            string newImgUrl = CollectClass.DefiniteUrl(imgurl, this.commUrl);

            return match.Value.Replace(imgurl, newImgUrl);
        }

        /// <summary>
        /// 商品描述
        /// </summary>
        public string Content
        {
            get { 
                string content = CollectClass.GetBody(comHtmlCode, myCollectPropert.ContStartString, myCollectPropert.ContOverString, false, false);
                string pat = @"<img(?<Attributes1>[\s\S]*?)src=(""{1}|'{1}|)(?<picture>[^\[^>]*?(gif|jpg|jpeg|bmp|bmp))(""{1}|'{1}|)(?<Attributes2>[\s\S]*?)>";

                string result = Regex.Replace(content, pat,
                 new MatchEvaluator(GetContent), RegexOptions.IgnoreCase);

                return result ;
            }
            
        }

        /// <summary>
        /// 图片路径
        /// </summary>
        public string ImgPath
        {
            get
            {
                string imgurl = CollectClass.DefiniteUrl(CollectClass.GetBody(comHtmlCode, myCollectPropert.ImgStartString, myCollectPropert.ImgOverString, false, false), CommUrl);
                string ext = imgurl.Substring(imgurl.LastIndexOf("/") + 1);
                string imgPathFile = CollectSetting.Default["SaveImagePath"].ToString()  + ext;
                localImgPath = CollectClass.SaveRemoteFile(imgurl, imgPathFile);
                return imgurl;

                //return CollectClass.SaveRemoteFile(CollectClass.DefiniteUrl(imgurl, CommUrl), imgPathFile);
            }
        }

        

        /// <summary>
        /// 商品标题
        /// </summary>
        public string Title
        {
            get { return CollectClass.GetBody(comHtmlCode, myCollectPropert.TitleStartString, myCollectPropert.TitleOverString, false, false); }
        }
        
        /// <summary>
        /// 商品价格
        /// </summary>
        public double Price
        {
            get {
                if (myCollectPropert.PriceStartString == "" || myCollectPropert.PriceOverString == "")
                {
                    return 0;
                }
                return Convert.ToDouble(CollectClass.GetBody(comHtmlCode, myCollectPropert.PriceStartString, myCollectPropert.PriceOverString, false, false));
            }
                //return CollectClass.GetPrice(Convert.ToDouble(CollectClass.GetBody(comHtmlCode, myCollectPropert.PriceStart, myCollectPropert.PriceOver, false, false))); }
        }


        #endregion
    }
}
